﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Xml.Serialization;
using Microsoft.WowAddonStudio.FrameXml.Serialization.Enumerations;
using Microsoft.WowAddonStudio.FrameXml.Serialization.LayoutFrames;
using Microsoft.WowAddonStudio.FrameXml.Serialization.Support;

namespace Microsoft.WowAddonStudio.FrameXml.Serialization
{
    [Serializable]
    [DesignerCategory("code")]
    [XmlType(AnonymousType = true, Namespace = "http://www.blizzard.com/wow/ui/")]
    [DebuggerDisplay("Level = {Level}, Item Count = {Items.Count}")]
    public class Layer : SerializationObject, INamedObject, IInheritableObject<Layer>
    {
        private readonly PropertyBag propertyBag;
        private Layer inheritedLayer;

        /// <summary>
        /// Initializes a new instance of the <see cref="Layer"/> class.
        /// </summary>
        public Layer()
        {
            this.Level = DrawLayer.Artwork;
            propertyBag = new PropertyBag(this);
        }

        /// <summary>
        /// Gets or sets the level of the layer.
        /// </summary>
        [XmlAttribute("level")]
        [DefaultValue(DrawLayer.Artwork)]
        public DrawLayer Level { get; set; }

        /// <summary>
        /// Gets the name of the object.
        /// </summary>
        [XmlIgnore]
        public string Name
        {
            get { return Level.ToString(); }
        }

		/// <summary>
		/// Gets or sets a value indicating whether inheritance is enabled.
		/// </summary>
		/// <remarks>
		/// The value is propagated to all non-inherited children.
		/// </remarks>
		[XmlIgnore]
		[Browsable(false)]
		public bool InheritanceEnabled
		{
			get { return propertyBag.InheritanceEnabled; }
			set
			{
				propertyBag.InheritanceEnabled = value;
				if(inheritedLayer != null)
				{
					inheritedLayer.InheritanceEnabled = value;	
				}
			}
		}

        /// <summary>
        /// Gets or sets the inherited object.
        /// </summary>
        [XmlIgnore]
		[Browsable(false)]
        public Layer InheritedObject
        {
            get { return inheritedLayer; }
            set
            {
                inheritedLayer = value;
                propertyBag.InheritedPropertyBag = inheritedLayer.propertyBag;
            }
        }

        /// <summary>
        /// Gets or sets the items on the layer.
        /// </summary>
        /// <remarks>Only <see cref="Texture"/> and <see cref="FontString"/> items are allowed on a layer.</remarks>
        [XmlElement("Texture", typeof(Texture))]
        [XmlElement("FontString", typeof(FontString))]
        public InheritableList<LayeredLayoutFrame> Items
        {
            get { return propertyBag.GetInheritableList<LayeredLayoutFrame>("Items"); }
        }
    }
}